// CSE 142 Winter 2008, Marty Stepp // // This program draws several car figures on a DrawingPanel. // You must have DrawingPanel.java in the same folder as your program. import java.awt.*; public class DrawCar { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(800, 400); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); // Draw a line of cars, all size 100 for (int i = 0; i <= 6; i++) { drawCar(g, i * 110, 30, 100); } // Draw an additional car at (220, 100), size 50 drawCar(g, 220, 100, 50); } // This method draws a car figure at the given (x, y) // position, scaled to the given size. public static void drawCar(Graphics g, int x, int y, int size) { g.setColor(Color.BLACK); g.fillRect(x, y, size, size / 2); g.setColor(Color.RED); g.fillOval(x + size / 10, y + 2 * size / 5, size / 5, size / 5); g.fillOval(x + 7 * size / 10, y + 2 * size / 5, size / 5, size / 5); g.setColor(Color.CYAN); g.fillRect(x + 7 * size / 10, y + size / 10, 3 * size / 10, size / 5); } }